/*********************************************************
          File Name:polymorphism.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Sat 26 Sep 2015 04:05:30 PM CST
**********************************************************/

#include <iostream>
#include <string>

using namespace std;

class Base
{
        public:
                Base(string x): s(x) {}
                virtual void print()
                {
                        cout << s << endl;
                }
        private:
                string s;
};

class derived: public Base
{
        public:
                derived(string x, string y): Base(x), s(y) {}

                virtual void print() 
                {
                        //print();              // infinite recursion
                        //this->Base::print();    // no problem
                        cout << s << endl;
                }

        private:
                string s;
};

int main()
{
        Base B("this is Base");

        derived D("base", "derived");

        Base *pB = &D;          // static type of pB is Base, dynamic type is derived

        pB->print();          // base class call derived class's method

        pB->Base::print();      // avoid polymorphism

        B.print();             // static bind
}
